package leetcode;


import java.util.HashMap;
import java.util.HashSet;

/**
 * @author niann
 * @description TODO https://leetcode.cn/problems/linked-list-cycle/description/
 * @since 2024/11/13 21:33
 **/
public class LeetCode141 {

    public class ListNode {
        int val;
        ListNode next;
        ListNode(int x) {
            val = x;
            next = null;
        }
    }
    public class Solution {
        public boolean hasCycle(ListNode head) {
            if (head==null||head.next==null) return false;
            HashSet<ListNode> set = new HashSet<>();
            ListNode cur = head;
            while(cur!=null){
                if (!set.add(cur)) {
                    return true;
                }
                cur=cur.next;
            }
            return false;
        }
    }
}
